//引入接口
import API from '../../config/server.js'
Page({
  data:{
    voiceCate: [],
    yxid:0,
    voiceDetail:{},
    poster: '',
    name: '王者荣耀语音',
    author: '',
    src:'',
    audioCtx:''
  },
  onLoad()
  {
    this.voiceNav()
  },
  voiceNav(){
    var that=this
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: API.voiceNav,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        let data=res.data
        if (data.success){
          that.setData({
            voiceCate: data.data.dhty_e9,
            yxid: data.data.dhty_e9[0].yxid_a7
          })
          that.voiceDetail()
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '网络错误',
          icon: 'error',
        })
      },
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  changeCate(e){
    let id = e.currentTarget.dataset.id
    this.setData({
      yxid:id
    })
    this.voiceDetail()
  },
  voiceDetail() {
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    let yxid=this.data.yxid || 187
    wx.request({
      url: API.voiceDetail + '?id=' + yxid,
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        let data = res.data.data
        let type = Object.prototype.toString.call(data.yxpfyy_fe)
        let arr=[]
        if (/Object/.test(type)){
          arr.push(data.yxpfyy_fe)
          data.yxpfyy_fe = arr
        }
        
        // console.log(type)
        console.log(data.yxpfyy_fe)
        that.setData({
          voiceDetail: data
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '网络错误',
          icon: 'error',
        })
      },
      complete: function (res) {
        wx.hideLoading()
      },
    })
  },
  onReady(e)
  {
    //创建一个语音播放的对象
    this.audioCtx = wx.createAudioContext('myAudio',this)
  },
  play(e) {
    let voice=e.currentTarget.dataset.voice
    
    // this.audioCtx.src = 'https:' + voice
    this.setData({
      src: 'https:'+ voice
    })
    
    
    // this.audioCtx.play()
    if (this.audioCtx.autoplay){
      this.audioCtx.autoplay = false
      this.audioCtx.pause()
    }else{
      this.audioCtx.autoplay = true
      this.audioCtx.play()
    }
    
  }
})